package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationSeven;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BloodStasis extends Activity implements View.OnClickListener {

    private Button next6,whetherBloodStasis;
    private RadioGroup subcutaneousBleeding,zygomaticBloodshot,
                       bodyPain,dullComplexion,
                       darkCircles,easyToForget,
                       dimLips;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bloodstasis);
        //为控件绑定资源
        next6=findViewById(R.id.Next6);

                subcutaneousBleeding=findViewById(R.id.SubcutaneousBleeding);
                zygomaticBloodshot=findViewById(R.id.ZygomaticBloodshot);
                bodyPain=findViewById(R.id.BodyPain);
                dullComplexion=findViewById(R.id.DullComplexion);
                darkCircles=findViewById(R.id.DarkCircles);
                easyToForget=findViewById(R.id.EasyToForget);
                dimLips=findViewById(R.id.DimLips);


        //为控件设置监听器
        next6.setOnClickListener(this);

                    subcutaneousBleeding.setOnClickListener(this);
                    zygomaticBloodshot.setOnClickListener(this);
                    dullComplexion.setOnClickListener(this);
                    bodyPain.setOnClickListener(this);
                    darkCircles.setOnClickListener(this);
                    easyToForget.setOnClickListener(this);
                    dimLips.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Next6:{
                //计算结果,把结果写入缓存
                BloodStasisResult();
//                Toast.makeText(BloodStasis.this, "进入特禀质", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(BloodStasis.this,Idiosyncratic.class));
            }
            break;

        }

    }
    public void BloodStasisResult(){
        int  subcutaneousBleedingid=  subcutaneousBleeding.getCheckedRadioButtonId();
        RadioButton subcutaneousBleedingbtn= findViewById(subcutaneousBleedingid);
        String  subcutaneousBleedingfin= subcutaneousBleedingbtn.getText().toString();

        int     zygomaticBloodshotid=   zygomaticBloodshot.getCheckedRadioButtonId();
        RadioButton  zygomaticBloodshotbtn= findViewById( zygomaticBloodshotid);
        String  zygomaticBloodshotfin= zygomaticBloodshotbtn.getText().toString();


        int bodyPainid= bodyPain.getCheckedRadioButtonId();
        RadioButton bodyPainbtn= findViewById(bodyPainid);
        String bodyPainfin=bodyPainbtn.getText().toString();


        int  dullComplexionid=  dullComplexion.getCheckedRadioButtonId();
        RadioButton  dullComplexionbtn= findViewById(dullComplexionid);
        String  dullComplexionfin=   dullComplexionbtn.getText().toString();


        int  darkCirclesid=  darkCircles.getCheckedRadioButtonId();
        RadioButton  darkCirclesbtn= findViewById( darkCirclesid);
        String darkCirclesfin= darkCirclesbtn.getText().toString();

        int  easyToForgetid= easyToForget.getCheckedRadioButtonId();
        RadioButton  easyToForgetbtn= findViewById(easyToForgetid);
        String  easyToForgetfin= easyToForgetbtn.getText().toString();


        int   dimLipsid=    dimLips.getCheckedRadioButtonId();
        RadioButton    dimLipsbtn= findViewById( dimLipsid);
        String    dimLipsfin=   dimLipsbtn.getText().toString();






        Map<String,Integer> subcutaneousBleedingMap=new HashMap<String,Integer>();
        subcutaneousBleedingMap.put("没有",1);
        subcutaneousBleedingMap.put("很少",2);
        subcutaneousBleedingMap.put("有时",3);
        subcutaneousBleedingMap.put("常常",4);
        subcutaneousBleedingMap.put("经常",5);

        Map<String,Integer>  zygomaticBloodshotMap=new HashMap<String,Integer>();
        zygomaticBloodshotMap.put("没有",1);
        zygomaticBloodshotMap.put("很少",2);
        zygomaticBloodshotMap.put("有时",3);
        zygomaticBloodshotMap.put("常常",4);
        zygomaticBloodshotMap.put("经常",5);



        Map<String,Integer> bodyPainMap=new HashMap<String,Integer>();
        bodyPainMap.put("没有",1);
        bodyPainMap.put("很少",2);
        bodyPainMap.put("有时",3);
        bodyPainMap.put("常常",4);
        bodyPainMap.put("经常",5);

        Map<String,Integer>  dullComplexionMap=new HashMap<String,Integer>();
        dullComplexionMap.put("没有",1);
        dullComplexionMap.put("很少",2);
        dullComplexionMap.put("有时",3);
        dullComplexionMap.put("常常",4);
        dullComplexionMap.put("经常",5);

        Map<String,Integer> darkCirclesMap=new HashMap<String,Integer>();
        darkCirclesMap.put("没有",1);
        darkCirclesMap.put("很少",2);
        darkCirclesMap.put("有时",3);
        darkCirclesMap.put("常常",4);
        darkCirclesMap.put("经常",5);

        Map<String,Integer>  easyToForgetMap=new HashMap<String,Integer>();
        easyToForgetMap.put("没有",1);
        easyToForgetMap.put("很少",2);
        easyToForgetMap.put("有时",3);
        easyToForgetMap.put("常常",4);
        easyToForgetMap.put("经常",5);

        Map<String,Integer> dimLipsMap=new HashMap<String,Integer>();
        dimLipsMap.put("没有",1);
        dimLipsMap.put("很少",2);
        dimLipsMap.put("有时",3);
        dimLipsMap.put("常常",4);
        dimLipsMap.put("经常",5);







        Integer bloodStasisresult = subcutaneousBleedingMap.get(subcutaneousBleedingfin)+
                zygomaticBloodshotMap.get(zygomaticBloodshotfin) +
                bodyPainMap.get(  bodyPainfin)+
                dullComplexionMap.get(dullComplexionfin) +
                darkCirclesMap.get( darkCirclesfin)+
                easyToForgetMap.get(  easyToForgetfin)+
                dimLipsMap.get (dimLipsfin);

        ScoreCalculationSeven scoreCalculationSeven=new ScoreCalculationSeven(bloodStasisresult);
        String ConversionScore=scoreCalculationSeven.getResult();
        TextView bloodStasisResult=findViewById(R.id.BloodStasisResult);

        bloodStasisResult.setText("血瘀质结果:"+ConversionScore);
        //输出数据到文件

        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }


    }


    }

